// import { ApiResponse } from '@/types/data'
// import { ApiResponse } from '@/types/data'
import { ApiResponse, ArticleDetail } from '@/types/data'
import { RootThunkAction } from '@/types/store'
import request from '@/utils/request'
// 获取文章详情
export const getArticleDetail = (id: string):RootThunkAction => {
  return async (dispatch: any) => {
    const res = await request.get<ApiResponse<ArticleDetail>>('/articles/' + id)
    console.log('文章详情', res)

    dispatch({
      type: 'articleDetail/setArticleDetail',
      payload: res.data.data
    })
  }
}
// 获取评论信息
export const getCommentList = (id: string):RootThunkAction => {
  return async (dispatch: any) => {
    const res = await request.get<ApiResponse<Comment>>('/comments/', {
      params: {
        type: 'a',
        source: id
      }
    })
    console.log('评论信息', res)

    dispatch({
      type: 'articleDetail/setCommentList',
      payload: res.data.data
    })
  }
}
// 获取文章详情底部信息
/**
* @description:
* @param {type}   id 文章的编号
* @param {type}   attitude 当前的态度
* @return:
*/
export const likeArticle = (id:string, attitude:number):RootThunkAction => {
  return async dispatch => {
    if (attitude === 1) {
      // 当前是喜欢，点一下：取消喜欢
      await request.delete('/article/likings/' + id)
    } else {
      // 当前是不喜欢，点一下：喜欢
      await request.post('/article/likings', { target: id })
    }
    // 更新文章
    dispatch(getArticleDetail(id))
  }
}

/**
 * 给文章添加评论
 * @param id 文章的编号
 * @param content 评论的内容
 * @returns
 */
export const addComment = (id: string, content: string): RootThunkAction => {
  return async (dispatch) => {
    await request.post('/comments/', {
      target: id,
      content
    })

    // 重新获取评论
    dispatch(getCommentList(id))
  }
}
